//
//  SISSUploadCoopContractViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/17.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSUploadCoopContractViewController.h"
#import "SISSNetworkingManager+CoopProgress.h"


@interface SISSUploadCoopContractViewController ()

@end

@implementation SISSUploadCoopContractViewController

- (void)viewDidLoad {
    self.isHiddenDateChooseView = YES;
    [super viewDidLoad];
    
    if (self.intermediaryContractModel) {
        self.contractNoView.rightTextField.text = self.intermediaryContractModel.contractNum;
        self.remarkView.rightTextField.text = self.intermediaryContractModel.otherdesc;
        self.uploadImageView.imageUrlArr = self.intermediaryContractModel.image.mutableCopy;
    }
}

- (void)confirmButtonClickAction {
    [self.view endEditing:YES];
    
    if (self.contractNoView.rightTextField.text.length == 0) {
//        [self alertWithMessage:@"请输入合同编号"];
        [SISSTools showSVErrorTitle:@"请输入合同编号" andTime:2 andColor:YES];
        return;
    }
    if (!self.intermediaryContractModel && self.uploadImageView.images.count == 0) {
        [SISSTools showSVErrorTitle:@"请添加合同文本图片" andTime:2 andColor:YES];
        return;
    }
    NSString *otherdesc = self.remarkView.rightTextField.text ? self.remarkView.rightTextField.text : @"";
    
    if (self.intermediaryContractModel) {
        NSDictionary *parameters = @{
                                      @"id" : self.intermediaryContractModel.ID,
                                     @"abpId" : self.abpId,
                                     @"contractNum" : self.contractNoView.rightTextField.text,
                                     @"otherdesc" : otherdesc
                                     };
        if (self.contractType == 1) {
            [kNetworkManager updateCoopMiddleContractWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewWithPopBackClassType: self.isHouseCooperatonIn ? SISSProgressPopBackClassTypeHouseCooperation : SISSProgressPopBackClassTypePassengerCooperation];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
            
        } else if (self.contractType ==2) {
            [kNetworkManager updateCoopBusinessContractWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewWithPopBackClassType: self.isHouseCooperatonIn ? SISSProgressPopBackClassTypeHouseCooperation : SISSProgressPopBackClassTypePassengerCooperation];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
    } else {
        NSDictionary *parameters = @{
                                     @"abpId" : self.abpId,
                                     @"contractNum" : self.contractNoView.rightTextField.text,
                                     @"otherdesc" : otherdesc
                                     };
        if (self.contractType == 1) {
            [kNetworkManager insertCoopMiddleContractWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewWithPopBackClassType: self.isHouseCooperatonIn ? SISSProgressPopBackClassTypeHouseCooperation : SISSProgressPopBackClassTypePassengerCooperation];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
            
        } else if (self.contractType ==2) {
            [kNetworkManager insertCoopBusinessContractWithParameters:parameters images:self.uploadImageView.images success:^(id responseObject) {
                
                if ([responseObject[@"code"] integerValue] == 200) {
                    [self pushToSuccessViewWithPopBackClassType: self.isHouseCooperatonIn ? SISSProgressPopBackClassTypeHouseCooperation : SISSProgressPopBackClassTypePassengerCooperation];
                }
                
            } failed:^(NSError *error) {
                NSLog(@"%@",error);
            }];
        }
    }
    
}
@end
